﻿using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace BackOffice
{
    public class GridViewExportUtil
    {
        public static void Export(string fileName, GridView gv)
        {
            //HttpContext.Current.Response.Clear();
            //HttpContext.Current.Response.AddHeader(
            //    "content-disposition", string.Format("attachment; filename={0}", fileName));
            //HttpContext.Current.Response.ContentType = "application/ms-excel";

            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    //  Create a form to contain the grid
                    Table table = new Table();

                    //  add the header row to the table
                    if (gv.HeaderRow != null)
                    {
                        GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                        table.Rows.Add(gv.HeaderRow);
                    }

                    //  add each of the data rows to the table
                    foreach (GridViewRow row in gv.Rows)
                    {
                        GridViewExportUtil.PrepareControlForExport(row);
                        table.Rows.Add(row);
                    }

                    //  add the footer row to the table
                    if (gv.FooterRow != null)
                    {
                        GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                        table.Rows.Add(gv.FooterRow);
                    }
                    gv.HeaderRow.Style.Add("background-color", "#FFFFFF");
                    
                    //Applying stlye to gridview header cells
                    for (int i = 0; i < gv.HeaderRow.Cells.Count; i++)
                    {
                        gv.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
                        gv.HeaderRow.Cells[i].Style.Add("border-style", "solid");
                    }

                    int j = 1;
                    //This loop is used to apply stlye to cells based on particular row
                    foreach (GridViewRow gvrow in gv.Rows)
                    {
                        //gvrow.BackColor = Color.White;
                        if (j <= gv.Rows.Count)
                        { 
                            if (j % 2 != 0)
                            {
                                for (int k = 0; k < gvrow.Cells.Count; k++)
                                {
                                    gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                                }
                            }
                        }
                        j++;
                    }

                    int m = 1;
                    //This loop is used to apply stlye to cells based on particular row
                    foreach (GridViewRow gvrow in gv.Rows)
                    {
                        //gvrow.BackColor = Color.White;
                        if (m <= gv.Rows.Count)
                        {                            
                                for (int k = 0; k < gvrow.Cells.Count; k++)
                                {
                                    gvrow.Cells[k].Style.Add("border-style", "solid");
                                }                            
                        }
                        m++;
                    }
                    //  render the table into the htmlwriter
                    table.RenderControl(htw);

                    //  render the htmlwriter into the response
                    //HttpContext.Current.Response.Write(sw.ToString());
                    //HttpContext.Current.Response.End();
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
                    HttpContext.Current.Response.ContentType = "application/ms-excel";
                    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    //render the htmlwriter into the response 
                    HttpContext.Current.Response.Write(sw.ToString());
                    HttpContext.Current.Response.End();  
                }
            }
        }

        /// <summary>
        /// Replace any of the contained controls with literals
        /// </summary>
        /// <param name="control"></param>
        private static void PrepareControlForExport(Control control)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                Control current = control.Controls[i];
                if (current is LinkButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
                }
                else if (current is ImageButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
                }
                else if (current is HyperLink)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
                }
                else if (current is DropDownList)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
                }
                else if (current is CheckBox)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
                }

                if (current.HasControls())
                {
                    GridViewExportUtil.PrepareControlForExport(current);
                }
            }
        }
    }
}